//
//  DetailsViewController.m
//  B段项目
//
//  Created by dllo on 15/9/24.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "DetailsViewController.h"
#import "PrefixHeader.pch"
#import "FirstModel.h"
#import "DataBaseHandle.h"
#import "CollcetModel.h"
#import <RESideMenu.h>

@interface DetailsViewController () <UIWebViewDelegate>

@property (nonatomic, assign) NSInteger i;


@end

@implementation DetailsViewController

- (void)viewWillAppear:(BOOL)animated
{
    SelfNavBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self collect];
    [self webViewSubviews];
    
    self.collectButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStyleDone target:self action:@selector(collectButtonClicked:)];
    self.navigationItem.rightBarButtonItem = self.collectButton;
    if (self.count == 1) {
        [self.collectButton setImage:[UIImage imageNamed:@"shoucang"]];
    }else{
        [self.collectButton setImage:[UIImage imageNamed:@"iconfont-collect.png"]];
    }
}

#pragma mark -- 判断收藏状态
- (void)collect
{
    [[DataBaseHandle shareDataBaseHandle] open];
    NSArray *array =[NSArray array];
    array = [[DataBaseHandle shareDataBaseHandle] selecttCollectWithAll];
    CollcetModel *model = [[CollcetModel alloc] init];
    for (int i = 0; i < array.count; i++) {
        model = [array objectAtIndex:i];
        if ([model.title isEqualToString:self.firstModel.newsTitle]) {
            self.count = 1;
            break;
        }else{
            self.count = 0;
        }
    }
    [[DataBaseHandle shareDataBaseHandle] close];
}

#pragma mark --点击收藏按键方法
- (void)collectButtonClicked:(UIBarButtonItem *)button
{
    [[DataBaseHandle shareDataBaseHandle] open];
    [[DataBaseHandle shareDataBaseHandle] createTable];
    CollcetModel *model = [[CollcetModel alloc] init];
    model.title = self.firstModel.newsTitle;
    model.url = self.firstModel.newsLink;
    model.newsImage = self.firstModel.newsImage;
   
    if (self.count == 1) {
        [[DataBaseHandle shareDataBaseHandle] deleteCollect:self.firstModel.newsTitle];
        self.count = 0;
    }else{
        [[DataBaseHandle shareDataBaseHandle] insertCollect:model];
        self.count = 1;
    }
    [[DataBaseHandle shareDataBaseHandle] close];
    if (self.count == 1) {
        [self.collectButton setImage:[UIImage imageNamed:@"shoucang"]];
    }else{
        [self.collectButton setImage:[UIImage imageNamed:@"iconfont-collect.png"]];
    }
}

- (void)webViewSubviews
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth , ScreenHeight + 10)];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.firstModel.newsLink]]];
    webView.scrollView.bounces = NO;
    webView.delegate = self;
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.i++;
    [self hideWaiting];
    SelfTabBar.hidden = YES;
}

//消除滚动轮指示器
-(void)hideWaiting
{
    //[[self.view viewWithTag:9999] removeFromSuperview];
    [[self.view viewWithTag:9998] removeFromSuperview];
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (self.i <= 0) {
        return YES;
    }else
        return NO;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
